package com.fire.eyes.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.fire.core.base.controller.BaseController;
import com.fire.core.mode.TMitUserToken;
import com.fire.core.service.CkeckTokenService;
import com.fire.core.util.AjaxResult;
import com.fire.core.util.BlogUtil;
import com.fire.eyes.constants.ErrorCodeConstants;
import com.fire.eyes.model.*;
import com.fire.eyes.service.*;
import com.fire.eyes.validation.*;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 *
 * @ClassName: MaintainController
 * @Description: 维保控制器
 * @author zhongzifeng
 * @date 2019/1/16 20:27
 *
 */
@Controller
public class MaintainController extends BaseController {
	private final static Logger blogin = Logger.getLogger(MaintainController.class);

	@Autowired
	private CkeckTokenService ckeckTokenService;
	@Autowired
	private IUserAuthorityService iUserAuthorityService;
	@Autowired
	private IFireSupervisorUserService iFireSupervisorUserService;
	@Autowired
	private IFireMaintainorService iFireMaintainorService;
	@Autowired
	private ISiteService iSiteService;


	/**
	 * 我管理的维保公司（列表）
	 * 此接口用于App向fire-app-server消除告警接口请求。
	 * @author zhongzifeng
	 * @param tokenDTO 传入参数
	 * @param bindingResult 参数校验结果
	 * @return
	 */
	@PostMapping(value = "/mymaitainors")
	@ResponseBody
	public AjaxResult myMaitainors(@RequestBody @Validated TokenDTO tokenDTO, BindingResult bindingResult) {
		blogin.info("MaintainController.myMaitainors获取到的消息参数" + tokenDTO.toString());
		BlogUtil.inlog(JSON.toJSONString(tokenDTO),"","/fire/eyes/mymaitainors","myMaitainors","req");
		AjaxResult result;
		if (bindingResult.hasErrors()){
			for (FieldError fieldError : bindingResult.getFieldErrors()) {
				blogin.error("MaintainController.myMaitainors().error  错误为："+fieldError.getDefaultMessage());
				result = ajaxResult(ErrorCodeConstants.PARAM_WRONGFULL, fieldError.getDefaultMessage(), fieldError.getDefaultMessage());
				return result;
			}
			System.out.print("效验bu通过：）");
		}
		try {
			TMitUserToken token = ckeckTokenService.ckeckToken(tokenDTO.getToken());
			if(token == null){
				blogin.error("MaintainController.myMaitainors().error  错误为：无token记录");
				result = ajaxResult(ErrorCodeConstants.PARAM_WRONGFULL, "无token记录", "无token记录");
				return result;
			}
			Integer userId = token.getUserid();
			Wrapper<UserAuthorityModel> userAutorityModel = new EntityWrapper<>();
			userAutorityModel.eq("userid",userId);
			UserAuthorityModel userAuthority = iUserAuthorityService.selectOne(userAutorityModel);
			String suthority = userAuthority.getAuthority();
			// 监管单位
			String supervisorStr = "supervisor";
			if(!supervisorStr.equals(suthority)){
				result = ajaxResult(ErrorCodeConstants.SUCCESS, "操作成功","", "操作成功");
				BlogUtil.inlog(JSON.toJSONString(result),"","/fire/eyes/mymaitainors","myMaitainors","resp");
				return result;
			}
			Wrapper<FireSupervisorUserModel> supervisorUserWrapper = new EntityWrapper<>();
			supervisorUserWrapper.eq("userid",userId);
			FireSupervisorUserModel supervisorUser = iFireSupervisorUserService.selectOne(supervisorUserWrapper);
			String supervisor = supervisorUser.getSupervisor();

//			Wrapper<SiteModel> siteWrapper = new EntityWrapper<>();
//			siteWrapper.eq("supervisorid",supervisor);
			//List<SiteModel> siteList = iSiteService.selectList(siteWrapper);
			//Page<SiteModel> siteList = iSiteService.selectPage(myMaitainorsDTO.getPage(),myMaitainorsDTO.getPageSize(),siteWrapper);
//			Page<Map<String,Object>> sitePage = iSiteService.queryMyMaitainorsList(new Page<Map<String, Object>>(tokenDTO.getPage(),tokenDTO.getPagesize()),supervisor);
//			List siteList = sitePage.getRecords();

			Page<Map<String,Object>> maintainorPage = iFireMaintainorService.queryMyMaitainorsList(new Page<Map<String, Object>>(tokenDTO.getPage(), tokenDTO.getPagesize()), supervisor);
			result = ajaxResult(ErrorCodeConstants.SUCCESS, "操作成功",maintainorPage.getRecords(), "操作成功");
			BlogUtil.inlog(JSON.toJSONString(result),"","/fire/eyes/sitelist","sitelist","resp");
		} catch (Exception e){
			e.printStackTrace();
			blogin.error("MaintainController.sitelist().error  错误为：" + e);
			result = ajaxResult(ErrorCodeConstants.PARAM_WRONGFULL, "服务器开小差", "服务器开小差");
		}
		return result;
	}

	/**
	 * 我管理的维保公司
	 * 此接口用于App向fire-app-server消除告警接口请求
	 * @author zhongzifeng
	 * @param maintainorNoToken 传入参数
	 * @param bindingResult 参数校验结果
	 * @return
	 */
	@PostMapping(value = "/maitainordetails")
	@ResponseBody
	public AjaxResult maitainorDetails(@RequestBody @Validated MaintainorNoToken maintainorNoToken, BindingResult bindingResult) {
		blogin.info("MaintainController.maitainorDetails获取到的消息参数" + maintainorNoToken.toString());
		BlogUtil.inlog(JSON.toJSONString(maintainorNoToken),"","/fire/eyes/maitainordetails","maitainorDetails","req");
		AjaxResult result;
		if (bindingResult.hasErrors()){
			for (FieldError fieldError : bindingResult.getFieldErrors()) {
				blogin.error("MaintainController.maitainorDetails().error  错误为："+fieldError.getDefaultMessage());
				result = ajaxResult(ErrorCodeConstants.PARAM_WRONGFULL, fieldError.getDefaultMessage(), fieldError.getDefaultMessage());
				return result;
			}
			System.out.print("效验bu通过：）");
		}
		String maintainor = maintainorNoToken.getMaintainor();
		Map<String, Object> map = iFireMaintainorService.queryMaintainor(maintainor);
		result = ajaxResult(ErrorCodeConstants.SUCCESS, "操作成功",map, "操作成功");
		BlogUtil.inlog(JSON.toJSONString(result),"","/fire/eyes/maitainordetails","maitainorDetails","resp");
		return result;
	}


}
